/**
* @Title: warningResult_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年11月6日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningResultCtrl', ['$scope','$modal','CommunityService','$stateParams','WarningAddService','BaseURL','Modal','WarningScreenService',
	                                function($scope,$modal,CommunityService,$stateParams,WarningAddService,baseUrl,Modal,WarningScreenService){
		$scope.warningScreenService = WarningScreenService;
		$scope.communityService = CommunityService;
		CommunityService.initCodeListDatas();
		$scope.warnResultDetails = [];
		$scope.form = {};
		$scope.form.date = {};
		$scope.createResult = function() {
//			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningResultDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope: $scope,
//				  scope:scope,
				  controller: 'WarningResultDialogCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				 if (!$scope.warnResultDetails) {
					 $scope.warnResultDetails = [];
					}
				 item.warnNo = $stateParams.warnNo;
				 $scope.warnResultDetails.push(item);
	 	    });
		}
		$scope.deleteResult = function() {
			Modal.confirm("确认删除?",function(){
				if(!$scope.warnResultDetails){
					return;
				}
				angular.forEach($scope.warnResultDetails,function(data,index){
				   if(data.checked === true){
					   $scope.warnResultDetails.splice(index,1);
				   }
			   });
			});
		}
		var params = function (){
			var warnNo=null;
			if($stateParams.warnNo&&!angular.isUndefined($stateParams.warnNo)){
				warnNo = $stateParams.warnNo;
			}
			var opinion=null;
			if($scope.form.opinion&&!angular.isUndefined($scope.form.opinion)){
				opinion = $scope.form.opinion;
			}
			var traceStartDate=null;
			if($scope.form.date.start&&!angular.isUndefined($scope.form.date.start)){
				traceStartDate = $scope.form.date.start;
			}
			var traceEndDate=null;
			if($scope.form.date.end&&!angular.isUndefined($scope.form.date.end)){
				traceEndDate = $scope.form.date.end;
			}
			var params = {
					warnNo:warnNo,
					traceStartDate:traceStartDate,
					opinion:opinion,
					traceEndDate:traceEndDate,
					warnResultDetails:$scope.warnResultDetails,
					warnMarkTempDetails:WarningAddService.warningMarks
			}
			return params;
		}
		$scope.confirm = function (){
			var par = params();
			if(!par.opinion){
				Modal.alert("意见不能为空!");
				return null;
			}
			if(!par.traceStartDate||!par.traceEndDate){
				Modal.alert("后续跟踪时段不能为空!");
				return null;
			}
			WarningScreenService.getWarnResult(params()).
			success(function(res){
				WarningAddService.warningMarks = [];//初始化扣分参数
				Modal.alert(res.msg);
			});
		}
		//全选或者不全选
		$scope.checked = false;
		$scope.getResultIdList = function (checked){
			angular.forEach($scope.warnResultDetails,function(data,index){
				data.checked = checked;
			});
		}
		//扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
		   $scope.marking = function(mark,data,medicalCode,orgType){
			   var medicalCodeTemp=null;
				if(medicalCode&&!angular.isUndefined(medicalCode)){
					medicalCodeTemp = medicalCode;
				}
			   if(mark != 1 && medicalCodeTemp == null){
				   Modal.alert("请先增加医疗机构信息！");
					return null;
			   }else{
				   medicalCode = medicalCodeTemp;
			   }
			   markData(mark,data,medicalCode,orgType);//扣分数据参数加载
			   var modalInstance = $modal.open({
				      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
				      backdrop:'static',
					  size:'sm',
					  scope: $scope,
//					  scope:scope,
					  controller: 'WarningDeductionDialogCtrl'
				    });
				 //接收返回数据
				 modalInstance.result.then(function(item) {
					 if (!WarningAddService.warningMarks) {
						 WarningAddService.warningMarks = [];
						}
					 WarningAddService.warningMarks = item;
		 	    });
		   }
		   //扣分数据参数加载
		   var markData = function(mark,data,medicalCode,orgType){
			   WarningAddService.mark = mark;
			   WarningAddService.warningMarkData = {
					   orgType:orgType,//0.机构，1.药店
					   medicalCode:medicalCode,//medicalCode:所属机构编号
					   code:data.code,//code:编号
					   name:data.name,//name:名称
					   caseType:'01',//案件类型caseType：01.预警,02.稽核
					   caseNo:$stateParams.warnNo,//案件编号
					   happenDate:'2014-11-24',//案发日期
					   punishDateStart:'2014-11-24',// 惩罚起期
					   punishDateEnd:'2014-11-24',// 惩罚终期
					   warningMarks:WarningAddService.warningMarks//扣分项数据
					   };
		   }
	}]).
	controller('WarningResultDialogCtrl',
			['$scope','BaseURL','$modalInstance', 'Modal','CommunityService','WarningScreenService', function($scope, baseUrl,$modalInstance, Modal,CommunityService,WarningScreenService){
				$scope.warningScreenService = WarningScreenService;
				$scope.communityService = CommunityService;
				CommunityService.initCodeListDatas();
				WarningScreenService.getWarnMonitor();
				WarningScreenService.getWarnMonitorRule();
				$scope.form = {};
				//规则名称
				$scope.warnRuleList=[];				
				var monitorCode=null;
				$scope.changeMonitor = function(){
			   		  if(WarningScreenService.warnMonitors){
         	   			  monitorCode=$scope.form.monitorNo.split('#')[0];
			   			  changeRuleOnChangeMonitor();         	   			 
			   		  }else{
			   			monitorCode=null;
			   		  }
			   	  }
				 //改变监控类型改变规则名称
			   	  function changeRuleOnChangeMonitor(){
			   		var newWarnRuleList=[];
			   		angular.forEach(WarningScreenService.warnMonitorRules,function(data,index){
						   if(data.monitorNo==monitorCode){ 
							   newWarnRuleList.push(data);					  
						   }
					   });	
			   	 $scope.warnRuleList=newWarnRuleList;
			   	  }
				var params = function (){
					var monitorNo=null;
					if($scope.form.monitorNo&&!angular.isUndefined($scope.form.monitorNo)){
						monitorNo = $scope.form.monitorNo;
					}
					var ruleNo=null;
					if($scope.form.ruleNo&&!angular.isUndefined($scope.form.ruleNo)){
						ruleNo = $scope.form.ruleNo;
					}
					var amount=null;
					if($scope.form.amount&&!angular.isUndefined($scope.form.amount)){
						amount = $scope.form.amount;
					}
					var isCheck=null;
					if($scope.form.isCheck&&!angular.isUndefined($scope.form.isCheck)){
						isCheck = $scope.form.isCheck;
					}
					var itemCutpoint=null;
					if($scope.form.itemCutpoint&&!angular.isUndefined($scope.form.itemCutpoint)){
						itemCutpoint = $scope.form.itemCutpoint;
					}
					$scope.form.warnNo = '';
					$scope.form.monitorType = '';
					$scope.form.sumCutpoint = '';
					var monitorType = '';
					if(monitorNo){
						$scope.form.monitorType = monitorType = monitorNo.substr(monitorNo.indexOf('#')+1,monitorNo.length);
						monitorNo = monitorNo.substr(0, monitorNo.indexOf('#'));
					}
					var params = {
							warnNo:'',
							monitorNo:monitorNo,
							monitorType:monitorType,
							ruleNo:ruleNo,
							amount:amount,
							isCheck:isCheck,
							itemCutpoint:itemCutpoint,
							sumCutpoint:''
					}
					return params;
				}
				//保存
				$scope.confirm = function (){
					var par = params();
					if(!par.monitorNo){
						Modal.alert("监控类型不能为空！");
						return null;
					}
					if(!par.ruleNo){
						Modal.alert("规则名称不能为空！");
						return null;
					}					
					if(!/^[\d]*(\.\d{1,2})?$/.test(par.amount)){
						Modal.alert("不合理费用必须为数字！");
						return null;
					}
					if(!par.isCheck){
						Modal.alert("是否自查不能为空！");
						return null;
					}
					if(!/^[\d]*(\.\d{1,2})?$/.test(par.itemCutpoint)){
						Modal.alert("该项目查扣分必须为数字！");
						return null;
					}
//					WarningScreenService.getWarnResult(params()).
//					success(function(res){
//						$scope.tableDatas = $scope.form;
					$scope.form.monitorNo = $scope.form.monitorNo.substr(0, $scope.form.monitorNo.indexOf('#'));
					     $modalInstance.close($scope.form);
//					});
				}
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
				//重置
				$scope.onReset = function (){
					$scope.form={};
				}
				
	}]).
	controller('WarningResultQueryCtrl',
			['$scope','$stateParams','BaseURL', 'Modal','WarningScreenService', function($scope,$stateParams, baseUrl, Modal,WarningScreenService){
				$scope.warningScreenService = WarningScreenService;
				var warnNo = "";
				if($stateParams.warnNo&&!angular.isUndefined($stateParams.warnNo)){
					warnNo = $stateParams.warnNo;
				}
				WarningScreenService.getWarnResultDetail({warnNo:warnNo}).
				success(function(res){
					$scope.warnResults = res.warnResults;
					$scope.warnResultDetails = res.warnResultDetails;
					var amount = "0";
					if(res.warnResultDetails){
						angular.forEach(res.warnResultDetails,function(data,index){
							amount = parseFloat(amount) + parseFloat(data.amount);
						   });
					}
					$scope.totalAmount = amount;
				});
	}]);
})();